package com.example.controller;

import com.example.getdata.DataFromJiaoTong;
import com.example.service.*;
import com.example.vo.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 *  @author 雨安
 *  类名： DataController
 *  创建时间：2024/4/1
 */
@RestController//控制器
@RequestMapping("api/data")//访问前缀
@Slf4j//日志
//@Api(tags = "运政数据一键获取")//前端查看api接口
public class DataController {
    /*********************************************************************************************/
    //此处装配所有要用的Service或接口对象

    @Autowired
    DataFromJiaoTong data;

    @Autowired
    PractitionerService practitionerService;

    @Autowired
    BusinessoperationService businessoperationService;

    @Autowired
    OccupationalrequirementsService occupationalrequirementsService;

    @Autowired
    HouseholdinformationService householdinformationService;

    @Autowired
    HeavygeneralcargoService heavygeneralcargoService;

    @Autowired
    RoadtransportService roadtransportService;

    @Autowired
    RoadtransportvehiclesService roadtransportvehiclesService;


    /*********************************************************************************************/

    /**
     * 自动获取运政数据
     */
//    @Scheduled(cron = "0 45 09 * * *")
//    @ApiOperation(value = "运政数据一键获取")//swagger2查看
    @GetMapping("/get")//访问后缀名
    String data() throws JsonProcessingException {
      try{
          //调用 从业人员基础信息
          Practitioner();
          //调用 业户经营许可证基础信息
          Businessoperation();
          //调用 从业资格证信息
          Occupationalrequirements();
          //调用 业户基础信息
          Householdinformation();
          //调用 本市重型普货车辆专题基础信息
          Heavygeneralcargo();
          //调用 道路运输经营许可信息
          Roadtransport();
          //调用 道路运输车辆基础信息
          Roadtransportvehicles();
          //返回 执行是否成功
          return "数据录入成功";
      }catch (Exception e){
          e.printStackTrace();
          throw new RuntimeException("数据录入失败");
      }
    }

    /*********************************************************************************************/
    //所有调用方法区
    void Practitioner() throws JsonProcessingException {
        try{
            //定义第一次访问的页码
            Integer page = 1;
            //调用数据获取方法
            Boolean mybit = getPractitioner(page);
            //循环遍历
            while (mybit == false){
                //每次访问页数+1
                page+=1;
                //我的循环判断值每次重新赋值
                mybit = getPractitioner(page);
            }
            System.out.println("数据正常录入完毕");
        }catch (Exception e){
            throw new RuntimeException("数据录入异常");
        }
    }

    //创建判断方法
    public Boolean getPractitioner(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData7(page.toString(),"1000","500222");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        PractitionerVo practitionerVo = objectMapper.readValue(str, PractitionerVo.class);
        //打印提示
        System.out.println("从业人员基础信息500222录入条数：" + practitionerVo.getData().getList().size());
        //执行添加sql语句
        practitionerService.saveBatch(practitionerVo.getData().getList());
        //返回查询数据
        return (page*1000) > practitionerVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/

    void Businessoperation() throws JsonProcessingException {
        try{
            //定义第一次访问的页码
            Integer page = 1;
            //调用数据获取方法
            Boolean mybit = getBusinessoperation(page);
            //循环遍历
            while (mybit == false){
                //每次访问页数+1
                page+=1;
                //我的循环判断值每次重新赋值
                mybit = getBusinessoperation(page);
            }
            System.out.println("交通局数据正常录入完毕");

            //定义第一次访问的页码
            Integer page2 = 1;
            //调用数据获取方法
            Boolean mybit2 = getBusinessoperation2(page2);
            //循环遍历
            while (mybit2 == false){
                //每次访问页数+1
                page2+=1;
                //我的循环判断值每次重新赋值
                mybit2 = getBusinessoperation2(page2);
            }
            System.out.println("运管处数据正常录入完毕");
        }catch (Exception e){
            throw new RuntimeException("数据录入异常");
        }
    }

    //创建判断方法
    public Boolean getBusinessoperation(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData6(page.toString(),"1000","綦江区交通局");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        BusinessoperationVo businessoperationVo = objectMapper.readValue(str, BusinessoperationVo.class);
        //打印提示
        System.out.println("业户经营许可证基础信息交通局录入数据条数：" + businessoperationVo.getData().getList().size());
        //执行添加sql语句
        businessoperationService.saveBatch(businessoperationVo.getData().getList());
        //返回查询数据
        return (page*1000) > businessoperationVo.getData().getTotalItem() ? true:false;
    }

    //创建判断方法
    public Boolean getBusinessoperation2(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData6(page.toString(),"1000","綦江区运管处");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        BusinessoperationVo businessoperationVo = objectMapper.readValue(str, BusinessoperationVo.class);
        //打印提示
        System.out.println("业户经营许可证基础信息运管处录入数据条数：" + businessoperationVo.getData().getList().size());
        //执行添加sql语句
        businessoperationService.saveBatch(businessoperationVo.getData().getList());
        //返回查询数据
        return (page*1000) > businessoperationVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/

    void Occupationalrequirements() throws JsonProcessingException {
        //定义第一次访问的页码
        Integer page = 1;
        //调用数据获取方法
        Boolean mybit = getOccupationalrequirements(page);
        //循环遍历
        while (mybit == false){
            //每次访问页数+1
            page+=1;
            //我的循环判断值每次重新赋值
            mybit = getOccupationalrequirements(page);
        }
        System.out.println("数据正常录入完毕");
    }

    //创建判断方法
    public Boolean getOccupationalrequirements(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData5(page.toString(),"1000","500222");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        OccupationalrequirementsVo occupationalrequirementsVo = objectMapper.readValue(str, OccupationalrequirementsVo.class);
        //打印提示
        System.out.println("从业资格证信息500222录入数据条数：" + occupationalrequirementsVo.getData().getList().size());
        //执行添加sql语句
        occupationalrequirementsService.saveBatch(occupationalrequirementsVo.getData().getList());
        //返回查询数据
        return (page*1000) > occupationalrequirementsVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/

    void Householdinformation() throws JsonProcessingException {
        //定义第一次访问的页码
        Integer page = 1;
        //调用数据获取方法
        Boolean mybit = getHouseholdinformation(page);
        //循环遍历
        while (mybit == false){
            //每次访问页数+1
            page+=1;
            //我的循环判断值每次重新赋值
            mybit = getHouseholdinformation(page);
        }
        System.out.println("数据正常录入完毕");
    }

    //创建判断方法
    public Boolean getHouseholdinformation(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData4(page.toString(),"1000","500222");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        HouseholdinformationVo householdinformationVo = objectMapper.readValue(str, HouseholdinformationVo.class);
        //打印提示
        System.out.println("业户基础信息500222录入数据条数：" + householdinformationVo.getData().getList().size());
        //执行添加sql语句
        householdinformationService.saveBatch(householdinformationVo.getData().getList());
        //返回查询数据
        return (page*1000) > householdinformationVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/
    void Heavygeneralcargo() throws JsonProcessingException {
        //定义第一次访问的页码
        Integer page = 1;
        //调用数据获取方法
        Boolean mybit = getHeavygeneralcargo(page);
        //循环遍历
        while (mybit == false){
            //每次访问页数+1
            page+=1;
            //我的循环判断值每次重新赋值
            mybit = getHeavygeneralcargo(page);
        }
        System.out.println("数据正常录入完毕");
    }

    //创建判断方法
    public Boolean getHeavygeneralcargo(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData2(page.toString(),"1000","500222");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        HeavygeneralcargoVo heavygeneralcargoVo = objectMapper.readValue(str, HeavygeneralcargoVo.class);
        //打印提示
        System.out.println("本市重型普货车辆专题基础信息500222录入数据条数：" + heavygeneralcargoVo.getData().getList().size());
        //执行添加sql语句
        heavygeneralcargoService.saveBatch(heavygeneralcargoVo.getData().getList());
        //返回查询数据
        return (page*1000) > heavygeneralcargoVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/
    void Roadtransport() throws JsonProcessingException {
        try{
            //定义第一次访问的页码
            Integer page = 1;
            //调用数据获取方法
            Boolean mybit = getRoadtransport(page);
            //循环遍历
            while (mybit == false){
                //每次访问页数+1
                page+=1;
                //我的循环判断值每次重新赋值
                mybit = getRoadtransport(page);
            }
            System.out.println("交通局数据正常录入完毕");

            //定义第一次访问的页码
            Integer page2 = 1;
            //调用数据获取方法
            Boolean mybit2 = getRoadtransport2(page2);
            //循环遍历
            while (mybit2 == false){
                //每次访问页数+1
                page2+=1;
                //我的循环判断值每次重新赋值
                mybit2 = getRoadtransport2(page2);
            }
            System.out.println("运管处数据正常录入完毕");
        }catch (Exception e){
            throw new RuntimeException("数据录入异常");
        }
    }

    //创建判断方法
    public Boolean getRoadtransport(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData(page.toString(),"1000","綦江区交通局");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        RoadtransportVo roadtransportVo = objectMapper.readValue(str, RoadtransportVo.class);
        //打印提示
        System.out.println("业户经营许可证基础信息交通局录入数据条数：" + roadtransportVo.getData().getList().size());
        //执行添加sql语句
        roadtransportService.saveBatch(roadtransportVo.getData().getList());
        //返回查询数据
        return (page*1000) > roadtransportVo.getData().getTotalItem() ? true:false;
    }

    //创建判断方法
    public Boolean getRoadtransport2(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData(page.toString(),"1000","綦江区运管处");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        RoadtransportVo roadtransportVo = objectMapper.readValue(str, RoadtransportVo.class);
        //打印提示
        System.out.println("业户经营许可证基础信息运管处录入数据条数：" + roadtransportVo.getData().getList().size());
        //执行添加sql语句
        roadtransportService.saveBatch(roadtransportVo.getData().getList());
        //返回查询数据
        return (page*1000) > roadtransportVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/

    void Roadtransportvehicles() throws JsonProcessingException {
        //定义第一次访问的页码
        Integer page = 1;
        //调用数据获取方法
        Boolean mybit = getRoadtransportvehicles(page);
        //循环遍历
        while (mybit == false){
            //每次访问页数+1
            page+=1;
            //我的循环判断值每次重新赋值
            mybit = getRoadtransportvehicles(page);
        }
        System.out.println("数据正常录入完毕");
    }

    //创建判断方法
    public Boolean getRoadtransportvehicles(Integer page) throws JsonProcessingException {
        //访问内部拿到指定的json字符串
        String str = this.data.getData3(page.toString(),"1000","500222");
        //json识别
        ObjectMapper objectMapper = new ObjectMapper();
        //转换
        RoadtransportvehiclesVo roadtransportvehiclesVo = objectMapper.readValue(str, RoadtransportvehiclesVo.class);
        //打印提示
        System.out.println("道路运输车辆基础信息500222录入数据条数：" + roadtransportvehiclesVo.getData().getList().size());
        //执行添加sql语句
        roadtransportvehiclesService.saveBatch(roadtransportvehiclesVo.getData().getList());
        //返回查询数据
        return (page*1000) > roadtransportvehiclesVo.getData().getTotalItem() ? true:false;
    }

    /*********************************************************************************************/


}
